package com.woody.aggregate.application.service.messagebox.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woody.aggregate.application.service.messagebox.IMessageboxApplicationConfigService;
import com.woody.aggregate.infrastructure.messagebox.entity.MessageboxApplicationConfig;
import com.woody.aggregate.infrastructure.messagebox.entity.MessageboxConfig;
import com.woody.aggregate.infrastructure.messagebox.mapper.MessageboxApplicationConfigMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woody.aggregate.infrastructure.messagebox.wrapmodel.MessageboxApplicationQuery;
import com.woody.aggregate.messagebox.dto.MessageboxAddDTO;
import com.woody.aggregate.messagebox.dto.MessageboxApplicationDTO;
import com.woody.aggregate.messagebox.dto.MessageboxEditDTO;
import com.woody.base.exception.BaseException;
import com.woody.common.sercurity.interceptor.UserInfoContext;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 弹框应用配置表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-19
 */
@Service
public class MessageboxApplicationConfigServiceImpl extends ServiceImpl<MessageboxApplicationConfigMapper, MessageboxApplicationConfig> implements IMessageboxApplicationConfigService {

    @Override
    public void updateConfig(MessageboxConfig messageboxConfig, MessageboxEditDTO dto) {

        List<MessageboxApplicationDTO> applications = dto.getApplications();
        this.baseMapper.delete(new QueryWrapper<MessageboxApplicationConfig>().lambda().eq(MessageboxApplicationConfig::getConfigId, messageboxConfig.getId()));
        //校验优先级是否重复
        List<MessageboxApplicationQuery> query = JSON.parseArray(JSON.toJSONString(dto.getApplications()), MessageboxApplicationQuery.class);
        List<MessageboxApplicationConfig> exist = this.baseMapper.queryListByPriorityAndTypeAndUrl(query);
        if(CollectionUtils.isNotEmpty(exist)){
            throw new BaseException("优先级重复");
        }

        List<MessageboxApplicationConfig> messageboxApplicationConfigs = new ArrayList<>();
        for (MessageboxApplicationDTO application : applications) {
            MessageboxApplicationConfig messageboxApplicationConfig = new MessageboxApplicationConfig();
            BeanUtils.copyProperties(application, messageboxApplicationConfig);
            messageboxApplicationConfig.setConfigId(messageboxConfig.getId());
            messageboxApplicationConfig.setCreateUserId(UserInfoContext.get().getUserId());
            messageboxApplicationConfig.setUpdateUserId(UserInfoContext.get().getUserId());
            messageboxApplicationConfigs.add(messageboxApplicationConfig);
        }
        this.baseMapper.insertBatch(messageboxApplicationConfigs);
    }

    @Override
    public void add(MessageboxAddDTO dto, MessageboxConfig messageboxConfig) {

        //校验优先级是否重复
        List<MessageboxApplicationQuery> query = JSON.parseArray(JSON.toJSONString(dto.getApplications()), MessageboxApplicationQuery.class);
        List<MessageboxApplicationConfig> exist = this.baseMapper.queryListByPriorityAndTypeAndUrl(query);
        if(CollectionUtils.isNotEmpty(exist)){
           throw new BaseException("优先级重复");
        }
        List<MessageboxApplicationConfig> messageboxApplicationConfigs = new ArrayList<>();
        for (MessageboxApplicationDTO application : dto.getApplications()) {
            MessageboxApplicationConfig messageboxApplicationConfig = new MessageboxApplicationConfig();
            BeanUtils.copyProperties(application, messageboxApplicationConfig);
            messageboxApplicationConfig.setConfigId(messageboxConfig.getId());
            messageboxApplicationConfig.setCreateUserId(UserInfoContext.get().getUserId());
            messageboxApplicationConfig.setUpdateUserId(UserInfoContext.get().getUserId());
            messageboxApplicationConfigs.add(messageboxApplicationConfig);
        }
        this.baseMapper.insertBatch(messageboxApplicationConfigs);
    }
}
